package com.tencent.liteav.demo.superplayer;

import android.app.Dialog;
import android.content.DialogInterface;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


import com.tencent.liteav.demo.superplayer.R;

import java.util.Objects;

/**
 * Created by sunxunchao on 2021/9/13.
 */
public class SpeedRateCard extends BottomSheetDialogFragment {

    private float speedLevel = 1.0f;
    private int mainColor = Color.parseColor("#FF4081");
    private RadioGroup radioGroup;
    private RadioButton rb_speed2, rb_speed15, rb_speed12, rb_speed1, rb_speed05;
    private TextView cancel;
    

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        BottomSheetDialog dialog = new BottomSheetDialog(getContext(), R.style.DialogStyle);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.card_speed_rate, container, false);
    }

    @Override
    public void onStart() {
        super.onStart();
        Objects.requireNonNull(getDialog()).getWindow().setLayout(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT
        );
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        radioGroup = view.findViewById(R.id.radioGroup);
        rb_speed2 = view.findViewById(R.id.rb_speed2);
        rb_speed15 = view.findViewById(R.id.rb_speed15);
        rb_speed12 = view.findViewById(R.id.rb_speed12);
        rb_speed1 = view.findViewById(R.id.rb_speed1);
        rb_speed05 = view.findViewById(R.id.rb_speed05);
        cancel = view.findViewById(R.id.cancel);

        if (getArguments() != null) {
            speedLevel = getArguments().getFloat("speedLevel");
            mainColor = getArguments().getInt("mainColor");
        }

        ColorStateList colorStateList = new ColorStateList(new int[][]
                {{-android.R.attr.state_checked},
                        {android.R.attr.state_checked}},
                new int[]{Color.parseColor("#13141A"), mainColor});
        rb_speed05.setTextColor(colorStateList);
        rb_speed1.setTextColor(colorStateList);
       rb_speed12.setTextColor(colorStateList);
       rb_speed15.setTextColor(colorStateList);
        rb_speed2.setTextColor(colorStateList);

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
            }
        });
        if (speedLevel == 0.5f) {
            rb_speed05.setChecked(true);
        } else if (speedLevel == 1.0f) {
            rb_speed1.setChecked(true);
        } else if (speedLevel == 1.2f) {
           rb_speed12.setChecked(true);
        } else if (speedLevel == 1.5f) {
           rb_speed15.setChecked(true);
        } else if (speedLevel == 2.0f) {
            rb_speed2.setChecked(true);
        }

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_speed05) {
                    rb_speed05.setChecked(true);
                    speedLevel = 0.5f;
                } else if (checkedId == R.id.rb_speed1) {
                    rb_speed1.setChecked(true);
                    speedLevel = 1.0f;
                } else if (checkedId == R.id.rb_speed12) {
                   rb_speed12.setChecked(true);
                    speedLevel = 1.2f;
                } else if (checkedId == R.id.rb_speed15) {
                   rb_speed15.setChecked(true);
                    speedLevel = 1.5f;
                } else if (checkedId == R.id.rb_speed2) {
                    rb_speed2.setChecked(true);
                    speedLevel = 2.0f;
                }
                dismissAllowingStateLoss();
            }
        });
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onResultListener != null) {
            onResultListener.onResult(speedLevel);
        }
    }


    private OnResultListener onResultListener;

    public void setOnResultListener(OnResultListener onResultListener) {
        this.onResultListener = onResultListener;
    }

    interface OnResultListener {
        void onResult(float speedLevel);
    }
}
